﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 反射
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //当没有对象的时候使用这种方式来获取某个类型的Type
            Type type = typeof(Person);

            //当已经获得对象后通过对象的GetType()方法来获取指定对象的类型的Type对象
            Person p = new Person();
            Type personType = p.GetType();

            //获取Person类中的所有的方法
            MethodInfo[] methods = personType.GetMethods();
            for (int i = 0; i < methods.Length; i++)
            {
                Console.WriteLine(methods[i].Name);
            }

            ///获取某个类型的所有属性
            PropertyInfo[] properties = personType.GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                Console.WriteLine(properties[i].Name);
            }


            ///获取类中的所有字段,私有字段无法获取
            FieldInfo[] fields = personType.GetFields();
            for (int i = 0; i < fields.Length; i++)
            {
                Console.WriteLine(fields[i].Name);
            }

            ///获取所有成员，不包含私有成员
            MemberInfo[] members = personType.GetMembers();
            for (int i = 0; i < members.Length; i++)
            {
                Console.WriteLine(members[i].Name);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Assembly assembly = Assembly.LoadFile(@"D:\MyStudy\NetDemo\委托\bin\Debug\委托.exe");
        }
    }
}
